package myta.bean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.model.SelectItem;

import myta.base.FormDataBase;
import myta.model.DataRow;
import myta.model.DataTable;
import myta.service.Command;
import myta.service.SQLexec;
import myta.service.Utility;

public class GuruData extends FormDataBase{
	private HtmlInputText txtidguru;
	private HtmlInputText txtnama;
	private HtmlInputText txtalamat;
	private HtmlInputText txttelepon;
	private HtmlSelectOneMenu txtjeniskelamin;
	private HtmlInputText txttmplahir;
	private HtmlInputText txttgllahir;
	private HtmlSelectOneMenu txtagama;
	private HtmlInputText txtpendidikanterakhir;
	private HtmlInputText txtthnmulaimengajar;
	private HtmlInputText txtstatusguru;
	private HtmlInputText txtusername;
	private HtmlInputText txtpassword;
	
	private List<SelectItem> listAgama;
	private List<SelectItem> listJenisKelamin;
	
	@Override
	public void init() throws Exception {
		formName = "guru";
		
		txtidguru = new HtmlInputText();
		txtnama = new HtmlInputText();
		txtalamat = new HtmlInputText();
		txttelepon = new HtmlInputText();
		txtjeniskelamin = new HtmlSelectOneMenu();
		txttmplahir = new HtmlInputText();
		txttgllahir = new HtmlInputText();
		txtagama = new HtmlSelectOneMenu();
		txtpendidikanterakhir = new HtmlInputText();
		txtthnmulaimengajar = new HtmlInputText();
		txtstatusguru = new HtmlInputText();
		txtusername = new HtmlInputText();
		txtpassword = new HtmlInputText();
		
		listAgama = new ArrayList<SelectItem>();
		SelectItem item1 = new SelectItem();
		item1.setValue("Islam");
		item1.setLabel("Islam");
		SelectItem item2 = new SelectItem();
		item2.setValue("Kristen");
		item2.setLabel("Kristen");
		SelectItem item3 = new SelectItem();
		item3.setValue("Katolik");
		item3.setLabel("Katolik");
		SelectItem item4 = new SelectItem();
		item4.setValue("Budha");
		item4.setLabel("Budha");
		SelectItem item5 = new SelectItem();
		item5.setValue("Hindu");
		item5.setLabel("Hindu");
		
		listAgama.add(item1);
		listAgama.add(item2);
		listAgama.add(item3);
		listAgama.add(item4);
		listAgama.add(item5);
		
		listJenisKelamin = new ArrayList<SelectItem>();
		SelectItem iteml = new SelectItem();
		iteml.setValue("L");
		iteml.setLabel("Laki-laki");
		
		SelectItem itemp = new SelectItem();
		itemp.setValue("P");
		itemp.setLabel("Perempuan");
		
		listJenisKelamin.add(iteml);
		listJenisKelamin.add(itemp);
	}

	@Override
	public void generateID() throws Exception {
		String sql = "call idguru()";
		Command command = SQLexec.makeCommand(sql);
		DataTable datatable = command.executeQuery();
		DataRow datarow = datatable.getRow(0);
		txtidguru.setValue(datarow.getString("id"));
	}

	@Override
	public void getRequestData(Map<String, String> map) throws Exception {
		if(map.containsKey("idguru")){
			String idguru = map.get("idguru");
			
			String sql = "select * from guru where idguru = ? order by idguru desc";
			Command command = SQLexec.makeCommand(sql);
			command.setObject(1, idguru);
			DataTable datatable = command.executeQuery();
			DataRow datarow = datatable.getRow(0);
			
			txtidguru.setValue(datarow.getString("idguru"));
			txtnama.setValue(datarow.getString("nama"));
			txtalamat.setValue(datarow.getString("alamat"));
			txttelepon.setValue(datarow.getString("telepon"));
			txtjeniskelamin.setValue(datarow.getString("jenis_kelamin"));
			txttmplahir.setValue(datarow.getString("tmp_lahir"));
			txttgllahir.setValue(Utility.format(datarow.getDate("tgl_lahir")));
			txtagama.setValue(datarow.getString("agama"));
			txtusername.setValue(datarow.getString("username"));
			txtpassword.setValue(datarow.getString("passwd"));
			txtpendidikanterakhir.setValue(datarow.getString("pendidikan_terakhir"));
			txtthnmulaimengajar.setValue(Utility.format(datarow.getDate("thn_mulai_mengajar")));
			txtstatusguru.setValue(datarow.getString("status_guru"));
		}
	}

	@Override
	public boolean simpan() throws Exception {
		boolean boo = false;
		
		String sql = "INSERT INTO guru(idguru,nama,alamat,telepon,tmp_lahir,tgl_lahir,agama,jenis_kelamin,pendidikan_terakhir,thn_mulai_mengajar,status_guru,username,passwd) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?);";
		Command command = SQLexec.makeCommand(sql);
		command.setObject(1, txtidguru.getValue().toString());
		command.setObject(2, txtnama.getValue().toString());
		command.setObject(3, txtalamat.getValue().toString());
		command.setObject(4, txttelepon.getValue().toString());
		command.setObject(5, txttmplahir.getValue().toString());
		command.setObject(6, txttgllahir.getValue().toString());
		command.setObject(7, txtagama.getValue().toString());
		command.setObject(8, txtjeniskelamin.getValue().toString());
		command.setObject(9, txtpendidikanterakhir.getValue().toString());
		command.setObject(10, txtthnmulaimengajar.getValue().toString());
		command.setObject(11, txtstatusguru.getValue().toString());
		command.setObject(12, txtusername.getValue().toString());
		command.setObject(13, txtpassword.getValue().toString());
		boo = command.executeUpdate();
		
		if(boo){
			return true;
		}else{
			return false;
		}		
	}

	@Override
	public boolean ubah() throws Exception {
		boolean boo = false;
		
		String sql = "UPDATE guru SET nama=?,alamat=?,telepon=?,tmp_lahir=?,tgl_lahir=?,agama=?,jenis_kelamin=?,pendidikan_terakhir=?,thn_mulai_mengajar=?,status_guru=?,username=?,passwd=? where idguru = ?;";
		Command command = SQLexec.makeCommand(sql);
		command.setObject(1, txtnama.getValue().toString());
		command.setObject(2, txtalamat.getValue().toString());
		command.setObject(3, txttelepon.getValue().toString());
		command.setObject(4, txttmplahir.getValue().toString());
		command.setObject(5, txttgllahir.getValue().toString());
		command.setObject(6, txtagama.getValue().toString());
		command.setObject(7, txtjeniskelamin.getValue().toString());
		command.setObject(8, txtpendidikanterakhir.getValue().toString());
		command.setObject(9, txtthnmulaimengajar.getValue().toString());
		command.setObject(10, txtstatusguru.getValue().toString());
		command.setObject(11, txtusername.getValue().toString());
		command.setObject(12, txtpassword.getValue().toString());
		command.setObject(13, txtidguru.getValue().toString());
		boo = command.executeUpdate();
		
		if(boo){
			return true;
		}else{
			return false;
		}
	}

	public HtmlInputText getTxtnama() {
		return txtnama;
	}

	public void setTxtnama(HtmlInputText txtnama) {
		this.txtnama = txtnama;
	}

	public HtmlInputText getTxtalamat() {
		return txtalamat;
	}

	public void setTxtalamat(HtmlInputText txtalamat) {
		this.txtalamat = txtalamat;
	}

	public HtmlInputText getTxttelepon() {
		return txttelepon;
	}

	public void setTxttelepon(HtmlInputText txttelepon) {
		this.txttelepon = txttelepon;
	}

	public HtmlSelectOneMenu getTxtjeniskelamin() {
		return txtjeniskelamin;
	}

	public void setTxtjeniskelamin(HtmlSelectOneMenu txtjeniskelamin) {
		this.txtjeniskelamin = txtjeniskelamin;
	}

	public HtmlInputText getTxttmplahir() {
		return txttmplahir;
	}

	public void setTxttmplahir(HtmlInputText txttmplahir) {
		this.txttmplahir = txttmplahir;
	}

	public HtmlInputText getTxttgllahir() {
		return txttgllahir;
	}

	public void setTxttgllahir(HtmlInputText txttgllahir) {
		this.txttgllahir = txttgllahir;
	}

	public HtmlSelectOneMenu getTxtagama() {
		return txtagama;
	}

	public void setTxtagama(HtmlSelectOneMenu txtagama) {
		this.txtagama = txtagama;
	}

	public HtmlInputText getTxtusername() {
		return txtusername;
	}

	public void setTxtusername(HtmlInputText txtusername) {
		this.txtusername = txtusername;
	}

	public HtmlInputText getTxtpassword() {
		return txtpassword;
	}

	public void setTxtpassword(HtmlInputText txtpassword) {
		this.txtpassword = txtpassword;
	}
	
	public List<SelectItem> getListAgama() {
		return listAgama;
	}

	public void setListAgama(List<SelectItem> listAgama) {
		this.listAgama = listAgama;
	}

	public List<SelectItem> getListJenisKelamin() {
		return listJenisKelamin;
	}

	public void setListJenisKelamin(List<SelectItem> listJenisKelamin) {
		this.listJenisKelamin = listJenisKelamin;
	}

	public HtmlInputText getTxtpendidikanterakhir() {
		return txtpendidikanterakhir;
	}

	public void setTxtpendidikanterakhir(HtmlInputText txtpendidikanterakhir) {
		this.txtpendidikanterakhir = txtpendidikanterakhir;
	}

	public HtmlInputText getTxtthnmulaimengajar() {
		return txtthnmulaimengajar;
	}

	public void setTxtthnmulaimengajar(HtmlInputText txtthnmulaimengajar) {
		this.txtthnmulaimengajar = txtthnmulaimengajar;
	}

	public HtmlInputText getTxtidguru() {
		return txtidguru;
	}

	public void setTxtidguru(HtmlInputText txtidguru) {
		this.txtidguru = txtidguru;
	}

	public HtmlInputText getTxtstatusguru() {
		return txtstatusguru;
	}

	public void setTxtstatusguru(HtmlInputText txtstatusguru) {
		this.txtstatusguru = txtstatusguru;
	}

}
